﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
namespace Catering_Rules
{
	/// <summary>
	/// RawMaterial
	/// </summary>
	public partial class T_RawMaterial
	{
		private readonly Catering_Operation.D_RawMaterial dal=new Catering_Operation.D_RawMaterial();
		public T_RawMaterial()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			return dal.Exists(id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Catering_Model.E_RawMaterial model)
		{
			dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Catering_Model.E_RawMaterial model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Catering_Model.E_RawMaterial GetModel(int id)
		{
			
			return dal.GetModel(id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Catering_Model.E_RawMaterial GetModelByCache(int id)
		{
			
			string CacheKey = "RawMaterialModel-" + id;
			object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
						Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Catering_Model.E_RawMaterial)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Catering_Model.E_RawMaterial> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Catering_Model.E_RawMaterial> DataTableToList(DataTable dt)
		{
			List<Catering_Model.E_RawMaterial> modelList = new List<Catering_Model.E_RawMaterial>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Catering_Model.E_RawMaterial model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Catering_Model.E_RawMaterial();
					if(dt.Rows[n]["id"]!=null && dt.Rows[n]["id"].ToString()!="")
					{
						model.id=int.Parse(dt.Rows[n]["id"].ToString());
					}
					if(dt.Rows[n]["RAWName"]!=null && dt.Rows[n]["RAWName"].ToString()!="")
					{
					model.RAWName=dt.Rows[n]["RAWName"].ToString();
					}
					if(dt.Rows[n]["RAWTID"]!=null && dt.Rows[n]["RAWTID"].ToString()!="")
					{
						model.RAWTID=int.Parse(dt.Rows[n]["RAWTID"].ToString());
					}
					if(dt.Rows[n]["RAWID"]!=null && dt.Rows[n]["RAWID"].ToString()!="")
					{
						model.RAWID=int.Parse(dt.Rows[n]["RAWID"].ToString());
					}
					if(dt.Rows[n]["RAWIDS"]!=null && dt.Rows[n]["RAWIDS"].ToString()!="")
					{
						model.RAWIDS=int.Parse(dt.Rows[n]["RAWIDS"].ToString());
					}
					if(dt.Rows[n]["RAWSid"]!=null && dt.Rows[n]["RAWSid"].ToString()!="")
					{
						model.RAWSid=dt.Rows[n]["RAWSid"].ToString();
					}
					if(dt.Rows[n]["RAWWid"]!=null && dt.Rows[n]["RAWWid"].ToString()!="")
					{
						model.RAWWid=dt.Rows[n]["RAWWid"].ToString();
					}
					if(dt.Rows[n]["InDBid"]!=null && dt.Rows[n]["InDBid"].ToString()!="")
					{
						model.InDBid=dt.Rows[n]["InDBid"].ToString();
					}
					if(dt.Rows[n]["Back"]!=null && dt.Rows[n]["Back"].ToString()!="")
					{
					model.Back=dt.Rows[n]["Back"].ToString();
					}
					if(dt.Rows[n]["temp1"]!=null && dt.Rows[n]["temp1"].ToString()!="")
					{
					model.temp1=dt.Rows[n]["temp1"].ToString();
					}
					if(dt.Rows[n]["temp2"]!=null && dt.Rows[n]["temp2"].ToString()!="")
					{
					model.temp2=dt.Rows[n]["temp2"].ToString();
					}
                    if (dt.Rows[n]["RAWZS"] != null && dt.Rows[n]["RAWZS"].ToString() != "")
                    {
                        model.RAWZS = int.Parse(dt.Rows[n]["RAWZS"].ToString());
                    }
                    if (dt.Rows[n]["SafetyTips"] != null && dt.Rows[n]["SafetyTips"].ToString() != "")
                    {
                        model.SafetyTips = dt.Rows[n]["SafetyTips"].ToString();
                    }
                    if (dt.Rows[n]["UsefulPart"] != null && dt.Rows[n]["UsefulPart"].ToString() != "")
                    {
                        model.UsefulPart = decimal.Parse(dt.Rows[n]["UsefulPart"].ToString());
                    }
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.E_RawMaterial> GetAllList(int num)
        {
            List<Catering_Model.E_RawMaterial> listInfo = new List<Catering_Model.E_RawMaterial>();
            DataTable dt = GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Catering_Model.E_RawMaterial temp = new Catering_Model.E_RawMaterial();
                temp.id = Int32.Parse(dt.Rows[i]["id"].ToString());
                temp.RAWName = dt.Rows[i]["RAWName"].ToString();
                temp.RAWIDS = Int32.Parse(dt.Rows[i]["RAWIDS"].ToString());
                listInfo.Add(temp);
            }
            return listInfo;
        }


		#endregion  Method

        #region 扩展方法

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, string field)
        {
            return dal.GetList(strWhere, field);
        }
        /// <summary>
        /// 通过区域获取符合要求的原料数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public DataSet GetListByAreaID(string strWhere, string field)
        {
            return dal.GetListByAreaID(strWhere, field);
        }

        public DataTable GetRawMaterialById(int id, int Type)
        {
            return dal.GetRawMaterialByIdtoProc(id, Type);
        }

        public bool UpdateShiwuYingyangByIdAndNutrientName(string id, string name, string content)
        {
            bool flag = false;
            int num = dal.UpdateShiwuYingyangByIdAndNutrientName(id, name, content);
            if (num > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
    }
}

